clear 

use "$P_Data_Processed/monthly_station_data_e5.dta", clear

by year_month Post, sort: gener postal_n_stations = _N
gener postal_n_others = postal_n_stations-1
drop if Post==0

** defining treatment 

capture drop treat
gener treat = month_treat_period*treat_group
label variable treat "Adopter" 

**
** drop outlier observations (top and bottom 1%)
drop if mean_wh_margin<0
drop if mean_wh_margin>0.2
drop if mean_price<1.18
drop if mean_price>1.7
	

sort StID year_month
by StID: gener first_treat_month = year_month if treat==1 & treat[_n-1]==0
sort StID first_treat_month
by StID: replace first_treat_month = first_treat_month[1]
replace first_treat_month = 1000 if missing(first_treat_month)
	
* number of other adopters in your Postal code
by Post year_month, sort: egen n_compet_adopt = total(treat)
replace n_compet_adopt = n_compet_adopt - 1 if treat==1	
	
** generating simple IV - share of adopters in your brand
by Brand year_month, sort: gener n_brand = _N
by Brand year_month: egen n_treat = total(treat)

gener share_others_treated = (n_treat)/(n_brand-1) if treat==0
replace share_others_treated = (n_treat-1)/(n_brand-1) if treat==1

label variable n_brand "N Brand Stations"
label variable share_others_treated "Share Brand Adopters"
label variable postal_n_stations "N Competitors in ZIP"
label variable n_compet_adopt "N Competitors Adopting"

** results broken up by ZIP market structure 

* monopoly markets 
capture drop outcome_var
gener outcome_var = mean_wh_margin 
ivreghdfe mean_wh_margin  postal_n_stations n_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & postal_n_stations==1, absorb(year_month StID) cluster(Post) 
sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_G2",  tex(frag) dec(3) label nor2 replace keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)

 capture drop outcome_var
gener outcome_var = mean_price 
ivreghdfe mean_price  postal_n_stations n_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & postal_n_stations==1, absorb(year_month StID) cluster(Post) 
 sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_G2",  tex(frag) dec(3) label nor2 append  keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)
 
 * non-monopoly markets 
capture drop outcome_var
gener outcome_var = mean_wh_margin 
ivreghdfe mean_wh_margin  postal_n_stations n_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & postal_n_stations>1, absorb(year_month StID) cluster(Post) 
sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_G2",  tex(frag) dec(3) label nor2 append keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)

 capture drop outcome_var
gener outcome_var = mean_price 
ivreghdfe mean_price  postal_n_stations n_compet_adopt l_gdp pop_density med_age employed_share l_pop n_brand mean_temp sd_temp mean_precip sd_precip  (treat = share_others_treated) if year<2019 & postal_n_stations>1, absorb(year_month StID) cluster(Post) 
 sum outcome_var if e(sample)==1 & treat_group==0
scalar outcome_var_mean = r(mean)
 outreg2 using "$P_Tables/Table_G2",  tex(frag) dec(3) label nor2 append  keep(treat) addstat(Non-Adopter Mean Outcome,outcome_var_mean)
